package com.shine.demo.leetCode.leetCodeOld;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LeetCode {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(towSum(new int[]{2, 7, 11, 15}, 9)));
    }

    /**
     * 爬楼梯
     *
     * @param n
     * @return
     */
    public static int climbStairs(int n) {
        if (n == 1 || n == 2) return n;
        int a = 1;//a 表示第n-2个台阶的走法
        int b = 2;//b表示第n-1个台阶的走法,传统迭代
        int count = 0;
        for (int i = 3; i <= n; i++) {
            count = a + b;//累加结果
            //向下迭代
            a = b;//下次迭代的第n-2个台阶的走法等于上次迭代n-1个台阶的走法
            b = count;//下次迭代的第n-1个台阶的走法等于上次迭代的第n个台阶走法
        }
        return count;
    }

    /**
     * 斐波那契数列
     *
     * @param n
     * @return
     */
    public static int fib(int n) {
        if (n == 0 || n == 1) {
            return n;
        }
        int a = 0;
        int b = 1;
        for (int i = 2; i <= n; i++) {
            b = a + b;
            a = b - a;
            b = b % 1000000007;
        }
        return b;
    }

    /**
     * 两数之和
     * @param nums
     * @param target
     * @return
     */
    public static int[] towSum(int[] nums, int target) {
        Map<Integer, Integer> keyMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (keyMap.containsKey(nums[i])) {
                return new int[]{i, keyMap.get(nums[i])};
            }
            keyMap.put(target - nums[i], i);
        }
        return null;
    }


}
